import { h } from 'vue';
import { BasicColumn, FormSchema } from '@/components/Table';
import { Switch } from 'ant-design-vue';
import { setRoleStatus } from '@/api/demo/system';
import { useMessage } from '@/hooks/web/useMessage';
import { getAllRoleList } from '@/api/demo/system';

type CheckedType = boolean | string | number;
export const columns: BasicColumn[] = [
  {
    title: '任务名称',
    dataIndex: 'roleName',
    align: 'left'
  },
  {
    title: 'AppId',
    dataIndex: 'appId',
    width: 180,
  },
  {
    title: '任务地址',
    dataIndex: 'roleValue',
    width: 300,
    align: 'left'
  },
  {
    title: '执行频率',
    dataIndex: 'roleValue',
    width: 200,
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 120,
    customRender: ({ record }) => {
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false;
      }
      return h(Switch, {
        checked: record.status === '1',
        checkedChildren: '停用',
        unCheckedChildren: '启用',
        loading: record.pendingStatus,
        onChange(checked: CheckedType) {
          record.pendingStatus = true;
          const newStatus = checked ? '1' : '0';
          const { createMessage } = useMessage();
          setRoleStatus(record.id, newStatus)
            .then(() => {
              record.status = newStatus;
              createMessage.success(`已成功修改角色状态`);
            })
            .catch(() => {
              createMessage.error('修改角色状态失败');
            })
            .finally(() => {
              record.pendingStatus = false;
            });
        },
      });
    },
  },
  {
    title: '最后执行时间',
    dataIndex: 'createTime',
    width: 180,
  },
  {
    title: '创建时间',
    dataIndex: 'id',
    width: 180,
    sorter: true,
    fixed: 'right',
    customRender: ({record})=>{
      return record.createTime;
    }
  },
];

export const searchLogFormSchema: FormSchema[] = [
  {
    field: '[startDate, endDate]',
    label: '日期范围',
    labelWidth: 80,
    component: 'RangePicker',
    colProps: { xs: 24, xl: 12, lg: 8, xxl: 6 },
    componentProps: {
      format: 'YYYY-MM-DD HH:mm:ss',
      placeholder: ['开始时间', '结束时间'],
      showTime: { format: 'HH:mm:ss' },
    },
    // defaultValue:[dayjs(new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 7)).format('YYYY-MM-DD'),dayjs(new Date()).format('YYYY-MM-DD')]
  },
  {
    field: 'status',
    label: '状态',
    component: 'Select',
    componentProps: {
      options: [
        { label: '启用', value: '1' },
        { label: '停用', value: '0' },
      ],
    },
    colProps: { xs: 24, md: 8, lg: 6 },
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'appId',
    label: 'AppId',
    component: 'ApiSelect',
    componentProps: {
      api: getAllRoleList,
      labelField: 'roleName',
      valueField: 'roleValue',
      showSearch: true,
      onOptionsChange: (options) => {
        console.log('get options', options.length, options);
      },
    },
    required: false,
    colProps: { xs: 24, md: 8, lg: 6 },
  },
  {
    field: 'name',
    label: '任务名称',
    component: 'Input',
    colProps: { xs: 24, md: 8, lg: 6 },
  },
  {
    field: 'status',
    label: '状态',
    component: 'Select',
    componentProps: {
      options: [
        { label: '启用', value: '1' },
        { label: '停用', value: '0' },
      ],
    },
    colProps: { xs: 24, md: 8, lg: 6 },
  },
];

export const formSchema: FormSchema[] = [
  {
    label: 'AppId',
    field: 'appId',
    component: 'ApiSelect',
    componentProps: {
      api: getAllRoleList,
      labelField: 'roleName',
      valueField: 'roleValue',
    },
    required: true,
  },
  {
    field: 'roleName',
    label: '任务名称',
    required: true,
    component: 'Input',
  },
  {
    field: 'roleValue',
    label: '任务Url',
    required: true,
    component: 'Input',
  },
  {
    field: 'cron',
    label: '执行频率',
    required: true,
    component: 'JEasyCron',
  },
  {
    field: 'method',
    label: '请求方式',
    component: 'RadioButtonGroup',
    defaultValue: 'get',
    componentProps: {
      options: [
        { label: 'GET', value: 'get' },
        { label: 'POST', value: 'post' },
      ],
    },
  },
  {
    field: 'status',
    label: '状态',
    component: 'RadioButtonGroup',
    defaultValue: '1',
    componentProps: {
      options: [
        { label: '启用', value: '1' },
        { label: '停用', value: '0' },
      ],
    },
  },
  {
    field: 'header',
    label: '请求Header',
    required: true,
    component: 'Input',
    defaultValue:'{"field":""}',
    slot: 'header',
    colProps: { xs: 24, md: 12 },
  },
  {
    field: 'body',
    label: '请求Body',
    required: true,
    component: 'Input',
    defaultValue:'{"field":""}',
    slot: 'body',
    colProps: { xs: 24, md: 12 },
  },
  
  {
    label: '备注',
    field: 'remark',
    component: 'InputTextArea',
  },
  {
    label: ' ',
    field: 'menu',
    slot: 'menu',
  },
];
